<?php
class AcademicSessionController extends AppController
{
    var $uses = array(
        'User'
    );

    # Set the layout to use
    var $layout = 'academic_session_layout';

    public function beforeFilter($param_array = null)
    {
        parent::beforeFilter();
    }


    function index($type = 'get'){
        $authUser = $this->Auth->user();
        $permissions = $this->action_permission;
        if ($this->request->is('ajax')) {
            $this->autoRender = false;
            $this->autoLayout = false;

            switch ($type) {
                case 'get' :
                    /**  Get posted data */
                    $page = isset($_POST['page']) ? $_POST['page'] : 1;
                    /** The current page */
                    $sortname = isset($_POST['sortname']) ? $_POST['sortname'] : 'id';
                    /** Sort column */
                    $sortorder = isset($_POST['sortorder']) ? $_POST['sortorder'] : 'asc';
                    /** Sort order */
                    $qtype = isset($_POST['qtype']) ? $_POST['qtype'] : '';
                    /** Search column */
                    $search_query = isset($_POST['query']) ? $_POST['query'] : '';
                    /** Search string */
                    $rp = isset($_POST['rp']) ? $_POST['rp'] : 10;

                    //Api Call
                    $results_api = $this->Api->post('/DataService/academic_sessions/get', array(
                        'page' => $page,
                        'sortname' => $sortname,
                        'sortorder' => $sortorder,
                        'qtype' => $qtype,
                        'query' => $search_query,
                        'rp' => $rp
                    ));

                    $data = $results_api['data'];
                    return json_encode($data);

                    break;

                case 'save' :
                    //Api Call
                    $results_api = $this->Api->post('/DataService/academic_sessions/save', array(
                        'id' => $_POST['id'],
                        'acc_year' => $_POST['acc_year']
                    ));
                    $response = $results_api['response_code'];
                    $message = $results_api['message'];

                    return json_encode(array('code' => $response, 'msg' => $message));

                    break;

                case 'delete':
                    //Api Call
                    $results_api = $this->Api->post('/DataService/academic_sessions/delete', array(
                        'ids' => $_POST['ids']
                    ));
                    $response = $results_api['response_code'];
                    $message = $results_api['message'];

                    return json_encode(array('code' => $response, 'msg' => $message));

                case 'set_default':
                    //Api Call
                    $results_api = $this->Api->post('/DataService/academic_sessions/set_default', array(
                        'id' => $_POST['id']
                    ));
                    $response = $results_api['response_code'];
                    $message = $results_api['message'];

                    return json_encode(array('code' => $response, 'msg' => $message));
            }
        }

        $page_title = 'Academic Sessions';
        $page_summary = 'Manage your academic sessions';
        $page_icon = 'iconfa-adjust';
        $this->set(compact('page_title','page_summary','page_icon','permissions'));

    }

}